package com.jzsm.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

import static java.util.Objects.requireNonNull;
import static org.apache.logging.log4j.util.Strings.trimToNull;

/**
 * SCM微服务工具类
 *
 * @author zhy
 */
@Slf4j
public class ScmUtil {
    /**
     * 获取集团 id
     *
     * @return 组织 id
     */
    public static Long getGroupId() {
        String result = getUriParam(ConstantsUtils.GROUP_ID_KEY);
        return result == null ? null : Long.valueOf(result);
    }

    /**
     * 获取组织ID
     * @return
     */
    public static String getOrgId() {
        return getUriParam(ConstantsUtils.ORG_ID_KEY);
    }
    /**
     * 获取uri参数
     *
     * @param paramName 参数名称
     * @return 参数结果，没有则为null
     */
    public static String getUriParam(String paramName) {
        try {
            ServletRequestAttributes sra = (ServletRequestAttributes) requireNonNull(RequestContextHolder.currentRequestAttributes());
            HttpServletRequest request = sra.getRequest();
            return trimToNull(request.getParameter(paramName));
        } catch (Throwable e) {
            return null;
        }
    }
}
